package com.one.apartmentsystem.app.controller;


import com.one.apartmentsystem.app.service.AlipayService;
import com.one.apartmentsystem.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 支付宝支付接口
 * @author mpk
 * @since 2024-8-19 14:22:16
 */
@Controller
@RequestMapping("/alipay")
@Slf4j
@CrossOrigin
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    /**
     * 创建支付
     * @param manageId 支付项目id
     * @return 跳转支付页面
     */
    @GetMapping("/create/{manageId}")
    public ResponseEntity<String> create(@PathVariable("manageId") String manageId) {
        // 发起支付
        String payUrl = alipayService.create(manageId);
        log.info(payUrl);

        // 拼合HTML
        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"en\" >\n" +
                "<head>\n" +
                "\t<meta charset=\"UTF-8\">\n" +
                "\t<title>支付</title>\n" +
                "</head>\n" +
                payUrl +
                "</html>\n";
        return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(html);
    }


    /**
     * 支付宝异步通知
     */
    @PostMapping("/notify")
    public void notify(HttpServletRequest request) {
        alipayService.notify(request);
    }

    /**
     * 退款
     */
    @PostMapping("/refund/{manageId}")
    public void refund(@PathVariable("manageId") String manageId) {
        log.info("退款中。。。");
        alipayService.refund(manageId);
    }
    /**
     * 获取账单
     */
    @GetMapping("/bill")
    @ResponseBody
    public Result bill() {
        String bill = alipayService.bill();
        return Result.ok(bill);
    }

}